<?php
class imageApi extends core
{
	public function verificationAction($arg)
	{
		include ROOT . '/libs/Zend/Captcha/Image.php';
		include ROOT . '/libs/Zend/Session/Namespace.php';
		
		$codeSession	= new Zend_Session_Namespace('code');
		$parm		= array(
			'font'		=> ROOT . 'libs/HoboStd.otf',
			'fontsize'	=> 24,
			'imgdir'	=> ROOT . 'libs/verification/',
			'session'	=> $codeSession,
			'width'		=> 150,
			'height'	=> 50,
			'wordlen'	=> 4,
		);
		if (!is_dir(ROOT . 'libs/verification/')) mkdir(ROOT . 'libs/verification/');
		
		
		$captcha	= new Zend_Captcha_Image($parm);
		
		$captcha->getGcFreq(3);
		$captcha->generate();
		
		$codeSession->code	= $captcha->getWord();
		header('Content-Type:image/png');
		die(file_get_contents($captcha->getImgDir().$captcha->getId().'.png'));
	}
	
	public function uploadAction($arg)
	{
		if($_FILES['Filedata']['error']!==0)
        {
            echo -1;
            return;
        }
        
        $website	= base::init('website')->fetchRow(array('wid'=>$arg['wid']));
		$attr		= base::init('website')->getConfig($website['domain']);
		
		$filename	= md5_file($_FILES['Filedata']['tmp_name']);
		$filetype	= strtolower(pathinfo($_FILES['Filedata']['name'],PATHINFO_EXTENSION));
		
		$dir		= $attr['fileurl'] . '/' .substr($filename,0,2).'/'.substr($filename,2,2);
		$filedir	= ROOT . $attr['arcdir'] . '/' . $dir.'/';
		$realdir	= ROOT . $attr['arcdir'] . '/' . $dir.'/'.$filename.'.'.$filetype;
		$srealdir	= ROOT . $attr['arcdir'] . '/' . $dir.'/s_'.$filename.'.'.$filetype;//缩略图路径

		$sfileurl	= '/'.$dir.'/s_'.$filename.'.'.$filetype;//缩略图url
		$fileurl	= '/'.$dir.'/'.$filename.'.'.$filetype;
		
		is_dir($filedir) || mdir($filedir);
		
        $result		= move_uploaded_file($_FILES['Filedata']['tmp_name'], $realdir);
        
        //生成缩略图
        require ROOT . 'libs/SimpleImage.php';

		$image	= new SimpleImage();
		$image->load($realdir);//上传的临时文件名
		$image->resizeToWidth(80);//设置宽度
		$image->save($srealdir);
        echo $result ? $sfileurl : '-1';
	}
}